/*
 * Copyright (c) 2009 Tim Mead
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */
 
/**
 * Update RSS feeds command.
 */
package com.misoneism.fire.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.xml.syndication.generic.IFeed;
	import com.adobe.xml.syndication.generic.IItem;
	import com.misoneism.fire.incident.Incident;
	import com.misoneism.fire.model.ModelLocator;
	import com.misoneism.fire.rssprocessor.RSSProcessor;
	import com.misoneism.fire.rssprocessor.RSSProcessorEvent;
	import com.misoneism.fire.rssprocessor.RSSProcessorResponder;
	
	import flash.events.IOErrorEvent;
	
	import mx.collections.ArrayCollection;
	
	public class UpdateRSSCommand
			implements ICommand
	{
		public function execute(event:CairngormEvent):void
		{
			var rssUrl:String = VictorianFireViewer.RSS_URL;
			
			var rssProcessor:RSSProcessor = new RSSProcessor();
			var rssProcessorResponder:RSSProcessorResponder = new RSSProcessorResponder();
			
			var ml:ModelLocator = ModelLocator.getInstance();
			
			// Track the responder for the RSS load
			ml.rssProcessorResponder = rssProcessorResponder;
			
			rssProcessorResponder.addEventListener(RSSProcessorEvent.ERROR_EVENT,
				function(rssEvent:RSSProcessorEvent):void
				{
					trace("FEED Error", rssEvent.data, rssUrl);
				});
				
			rssProcessorResponder.addEventListener(IOErrorEvent.IO_ERROR,
				function(ioEvent:IOErrorEvent):void
				{
					trace("IO Error", ioEvent, rssUrl);
				});
				
			rssProcessorResponder.addEventListener(RSSProcessorEvent.FEED_EVENT,
				function(rssEvent:RSSProcessorEvent):void
				{
					var feed:IFeed = rssEvent.data as IFeed;
					
					// Check we got some data
					if ((feed == null) || (feed.items == null) || (feed.items.length == 0))
					{
						// TODO: No data loaded
						return;
					}
					
					var ac:ArrayCollection = new ArrayCollection();
					
					for each (var ii:IItem in feed.items)
					{
						var i:Incident = Incident.deriveIncident(ii);
						
						if (i != null)
							ac.addItem(i);
					}
					
					// Save the incidents
					ModelLocator.getInstance().currentIncidents = ac;
					
					// Update the status
					ml.statusMessage = "Incidents updated.";
				});
			
			// Let the user know what's going on
			ml.statusMessage = "Loading RSS feed...";
				
			// Begin RSS acquisition
			rssProcessor.getFeed(rssProcessorResponder, rssUrl);
		}
	}
}